﻿using System;
using System.Diagnostics.Contracts;
using System.Linq;
using ContactPrinter.Model.Interfaces;

namespace ContactPrinter.Extension
{
    public static class ContactExtensions
    {
        public static int GetNumberOfLines(this IContact contact)
        {
            Contract.Requires<ArgumentNullException>(contact != null);

            return Max(1, contact.Organizations.Count, contact.Phonenumbers.Count, contact.PostalAddresses.Count, contact.Emails.Count);
        }

        public static IOrganization GetPrimaryOrFirstOrganization(this IContact contact)
        {
            if (contact.Organizations.Count <= 0) return null;
            return contact.Organizations.FirstOrDefault(organization => organization.Primary) ?? contact.Organizations.First();
        }

        private static int Max(params int[] values)
        {
            return values.Max();
        }
    }
}